package icedot.library.common.filesystem;


import java.io.File;

public class FileManager
{
	public static final String[]   s_pictureType = {"jpg","png"};
	public static final String[]   s_vidioType = {"mp4"};
	public static final String[]   s_privacy ={"lock"};
	
	public FileManager()
	{
		
	}

	/**
	 * 获取文件的类型
	 * @param path
	 * @return
     */
	public static int getFileInfoType(String path)
	{
		int where = path.lastIndexOf(".");
		if(where == -1)
			return FileInfo.s_typeUnkown;
		
		String type = path.substring(where + 1);
		for(String item : s_pictureType)
		{
			if(item.equals(type))
			{
				return FileInfo.s_typePic;
			}
		}
		
		for(String item: s_vidioType)
		{
			if(item.equals(type))
			{
				return FileInfo.s_typeVidio;
			}
		}
		
		for(String item: s_privacy)
		{
			if(item.equals(type))
			{
				String sPath = path.substring(0,where);
				switch(getFileInfoType(sPath))
				{
				case FileInfo.s_typePic:
					return FileInfo.s_typePicLock;
				case FileInfo.s_typeDoc:
					return FileInfo.s_typeDocLock;
				case FileInfo.s_typeVidio:
					return FileInfo.s_typeVidioLock;
				}
			}
		}
		return FileInfo.s_typeUnkown;
	}

	/**
	 * 获取文件的扩展名
	 * @param path
	 * @return
     */
	public static String getFileExtName(String path)
	{
		int where = path.lastIndexOf(".");
		if(where == -1)
		{
			return "";
		}
		return path.substring(where + 1);
	}

	public static String getFileName(String path)
	{
		int where = path.lastIndexOf(".");
		int namef = path.lastIndexOf("/");
		if(where == -1)
		{
			if(namef == -1)
			{
				return  path;
			}
			else
			{
				return path.substring(namef);
			}
		}
		else
		{
			if(namef == -1)
			{
				return path.substring(0,where);
			}
			else
			{
				return path.substring(namef + 1,where);
			}
		}
	}
	
	public static String getFileInfoName(String path,int type)
	{
		if(type >= FileInfo.s_typeDoc && type <= FileInfo.s_typeVidio)
		{
			int where = path.lastIndexOf(".");
			if(where == -1)
				return "";
			int namef = path.lastIndexOf("/");
			return path.substring(namef + 1,where);
		}
		else if(type >= FileInfo.s_typeDocLock && type <= FileInfo.s_typeVidioLock)
		{
			int where = path.lastIndexOf(".");
			if(where == -1)
				return "";
			String sPath = path.substring(0,where);
			where = sPath.lastIndexOf(".");
			int namef = sPath.lastIndexOf("/");
			return sPath.substring(namef + 1,where);
		}
		else
		{
			return "";
		}
	}

	/**
	 * 删除指定文件夹下的所有文件
	 * @param path
	 */
	public static void delDirFile(String path)
	{
		delDirFile(path,true);
	}

	/**
	 *  获取文件夹的大小
	 */
	public static long getDirSize(String path)
	{
		return getDirSize(path,true);
	}

	private static long getDirSize(String path,boolean isSubdir)
	{
		File file = new File(path);
		if(file.isFile())
		{
			return file.length();
		}
		else
		{
			if(file.isDirectory() && isSubdir && !file.isHidden() && file.list() !=null)
			{
				long ret = 0;
				for(String item : file.list())
				{
					String subPath = path + "/" +item;
					File temp = new File(subPath);
					if(temp.isFile())
					{
						ret += temp.length();
					}
					else if(temp.isDirectory() && isSubdir && !file.isHidden() && file.list() !=null)
					{
						ret += getDirSize(subPath,isSubdir);
					}
				}
				return ret;
			}
		}
		return 0;
	}

	private static void delDirFile(String path,boolean isSubdir)
	{
		File file = new File(path);
		if(file.isFile())
		{
			file.delete();
		}
		else
		{
			if(file.isDirectory() && isSubdir && !file.isHidden() && file.list() !=null)
			{
				for(String item : file.list())
				{
					String subPath = path + "/" +item;
					File temp = new File(subPath);
					if(temp.isFile())
					{
						temp.delete();
					}
					else
					{
						getDirSize(subPath,isSubdir);
						temp.delete();
					}
				}
			}
		}
	}
}
